package com.block.api.controller.expose;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.common.exception.NotifyExceptionService;
import com.block.common.service.LogFactory;
import com.block.merchant.service.IMerchantBaseInfoService;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.Charset;
import java.util.Map;

@RestController
@RequestMapping("apinotice/merchant")
public class NoticeMerchantController {

    private static final Logger log = LogFactory.getNoticeMerLog();

    @Autowired
    NotifyExceptionService notifyExceptionService;
    @Autowired
    IMerchantBaseInfoService merchantBaseInfoService;

    public static void main(String[] args) {

        String hostUrl = "https://api.longzankeji.cn";
        String hostUrlTest = "http://apitest.koukoudai.shop";
        String infoId = "105";
        System.out.println(StrUtil.format("{}/apinotice/merchant/notifyResult/{}", hostUrl, infoId));
        System.out.println(StrUtil.format("{}/apinotice/merchant/notifyEnter/{}", hostUrl, infoId));
        System.out.println(StrUtil.format("{}/apinotice/merchant/notifyPullData/{}", hostUrl, infoId));

        System.out.println(StrUtil.format("{}/apinotice/merchant/notifyResult/{}", hostUrlTest, infoId));
        System.out.println(StrUtil.format("{}/apinotice/merchant/notifyEnter/{}", hostUrlTest, infoId));
        System.out.println(StrUtil.format("{}/apinotice/merchant/notifyPullData/{}", hostUrlTest, infoId));

        /**
         * https://api.longzankeji.cn/apinotice/merchant/notifyResult/83
         *
         * https://api.longzankeji.cn/apinotice/merchant/notifyEnter/83
         */
    }

    /**
     * 授权结果通知
     *
     * @param request
     * @param infoId
     * @return
     */
    @RequestMapping(value = "/notifyResult/{infoId}", method = {RequestMethod.POST, RequestMethod.GET})
    public String notifyResult(HttpServletRequest request, @PathVariable("infoId") String infoId) {

        Map<String, String[]> parameterMap = null;
        String paramContent = null;
        try {
            parameterMap = request.getParameterMap();
            paramContent = IoUtil.read(request.getInputStream(), Charset.defaultCharset());
            log.info("【商户回调通知】【infoId】{}【parameterMap】{}【paramContent】{}", infoId, JSON.toJSONString(parameterMap), paramContent);

            JSONObject jsonObject = null;
            return JSON.toJSONString(jsonObject);
        } catch (Exception exception) {
            String content = StrUtil.format("【infoId】{}【parameterMap】{}【paramContent】{}", infoId, JSON.toJSONString(parameterMap), paramContent);
            notifyExceptionService.notifyNoticeInfo("MERCHANT_NOTICE_RESULT", "商户授权结果通知", content, exception);
        }
        return "exception";
    }

}
